package keeto.com.vn.classfied.model;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.widget.TextView;


public class ClickSpanParser {

	public static final String PATTERN_START = "[[\4\4";
	public static final String PATTERN_END = "\4\4]]";
	public static final String REGEX = "\\[\\[\\x04\\x04[\\s\\S]*?\\x04\\x04\\]\\]";
//	private static final String TAG = "ClickSpanParser";
	
	private static Pattern mPattern = null;
	
	/**
	 * 为TextView中的字符串用正则表达式处理，并将"@Username" 变成可以点击的链接
	 * 
	 * @param view 要设置链接的TextView
	 * @param charSequence Post的Message，数据源
	 * @param listener 监听生成的链接的点击时间的listener
	 */
	public static void clickifySnsPost(TextView view, CharSequence charSequence, final ClickSpan.OnClickListener listener) {

		SpannableStringBuilder text = new SpannableStringBuilder(charSequence);
		
		//build Regex matcher
		mPattern = Pattern.compile(REGEX);
		Matcher matcher = mPattern.matcher(text);
		
		/*
		 * 
		 */
		while (matcher.find()) {
			
			String clickableText = matcher.group();
//			Log.e(TAG,"****matched:"+clickableText);
			UserUIModel user = getSpannedData(clickableText);
			
			if(user==null){
				continue;
			}
			
			ClickSpan span = new ClickSpan(listener, user);
			
			int startIndex = text.toString().indexOf(clickableText);
			int endIndex = startIndex + clickableText.length();
			
			/*
			 */
			text.replace(startIndex, endIndex, "@"+user.getDisplayName());
			
			int end = startIndex + user.getDisplayName().length() + 1;
			
//			Log.e(TAG, "***start:"+startIndex+",end:"+end);
			
			if (startIndex == -1)
				continue;
			
			/*
			 * 
			 */
			text.setSpan(span, startIndex, end,
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			
		}
		
		view.setText(text);
		
		MovementMethod m = view.getMovementMethod();
		if ((m == null) || !(m instanceof LinkMovementMethod)) {
			view.setMovementMethod(LinkMovementMethod.getInstance());
		}
	}
	
	
	public static CharSequence getSnsClickify(CharSequence charSequence, final ClickSpan.OnClickListener listener) {

		SpannableStringBuilder text = new SpannableStringBuilder(charSequence);
		
		//build Regex matcher
		mPattern = Pattern.compile(REGEX);
		Matcher matcher = mPattern.matcher(text);
		
		/*
		 * 
		 */
		while (matcher.find()) {
			
			String clickableText = matcher.group();
//			Log.e(TAG,"****matched:"+clickableText);
			UserUIModel user = getSpannedData(clickableText);
			
			if(user==null){
				continue;
			}
			
			ClickSpan span = new ClickSpan(listener, user);
			
			int startIndex = text.toString().indexOf(clickableText);
			int endIndex = startIndex + clickableText.length();
			
			/*
			 */
			text.replace(startIndex, endIndex, "@"+user.getDisplayName());
			
			int end = startIndex + user.getDisplayName().length() + 1;
			
//			Log.e(TAG, "***start:"+startIndex+",end:"+end);
			
			if (startIndex == -1)
				continue;
			
			/*
			 * 
			 */
			text.setSpan(span, startIndex, end,
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			
		}
		
		return text;
	}

	/**
	 * 
	 */
	private static UserUIModel getSpannedData(String clickableText) {

		if (TextUtils.isEmpty(clickableText)) {
			return null;
		}

		int start = clickableText.indexOf(PATTERN_START);
		int end = clickableText.indexOf(PATTERN_END);

//		Log.e(TAG,"start:" + start + ",end:" + end);
		
		if (start >= end) {
			return null;
		}

		String data = clickableText.substring(start + PATTERN_START.length(),
				end);

//		Log.e(TAG,"data:" + data);
		
		try {

			JSONObject object = new JSONObject(data);
			UserUIModel aUser = new UserUIModel();
			aUser.setUsername(object.optString("username"));
			aUser.setUserId(object.optString("userId"));
			aUser.setDisplayName(object.optString("displayName"));
			return aUser;
			
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return null;
	}
	
}
